import Toast from '../../../component/Toast';
import { getLoginSession } from '../../../plugin/loginSession';
import { routerRedux } from 'dva/router';
import { pageUser } from '../service/userService';

const defaultState = {
	
	tab: '用户',
	userPageInfo: undefined,
	rolePageInfo: undefined,
	permissionPageInfo: undefined,
	channelPageInfo: undefined,
	
}

export default {

	namespace: 'system',

	state: {...defaultState},

	reducers: {
		
		reset(state, {tab}) {
			return {...defaultState}
		},
		
		setTab(state, {tab}) {
			return {...state, ...{tab}}
		},
		
		setUserPageInfo(state, {userPageInfo}) {
			return {...state, ...{userPageInfo}}
		},
		
		setRolePageInfo(state, {rolePageInfo}) {
			return {...state, ...{rolePageInfo}}
		},
		
		setPermissionPageInfo(state, {permissionPageInfo}) {
			return {...state, ...{permissionPageInfo}}
		},
		
		setChannelPageInfo(state, {channelPageInfo}) {
			return {...state, ...{channelPageInfo}}
		},
		
	},
	
	effects: {
		
		*init(action, {select, put}) {
			let loginSession = yield getLoginSession();
			if(!loginSession) {
				yield put(routerRedux.push('/login'));
				yield Toast.show('请登录');
			}
		},
		
		*pageUser({pageNum, pageSize}, {select, put}) {
			
			let res = yield pageUser({pageNum, pageSize});
			if (res.code == 1) {
				
			} else {
				res.msg && Toast.show(res.msg)
			}
		}
		
	}

}